package com.example.zhongcao.service.impl;

import com.example.zhongcao.entity.User;
import com.example.zhongcao.entity.UserSign;
import com.example.zhongcao.entity.Zhongcao;
import com.example.zhongcao.exception.InvalidParam;
import com.example.zhongcao.mapper.UserMapper;
import com.example.zhongcao.mapper.UserSignMapper;
import com.example.zhongcao.mapper.ZhongcaoMapper;
import com.example.zhongcao.service.SignService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SignServiceImpl implements SignService {
    @Resource
    UserMapper userMapper;
    @Resource
    UserSignMapper userSignMapper;
    @Resource
    ZhongcaoMapper zhongcaoMapper;
    private static final String userHadType="HAD_USER";
    private static final String zcHadType="HAD_ZC";
    @Override
    public void buildUserSign(String openid, String signname) throws InvalidParam {
        User user=userMapper.selectByOpenid(openid);
        if(user==null){
            throw new InvalidParam("openid无效");
        }
        UserSign us=userSignMapper.selectByOpenidAndName(openid,signname);
        if(us!=null){
            throw new InvalidParam("已经拥有该标签啦，再看看别的吧!");
        }
        userSignMapper.insertUserSign(openid, signname);
    }

    @Override
    public List<String> getSigns(String type, String openid, Integer zhongcaoid) throws InvalidParam {
        if(type==null || type.equals("")){
            throw new InvalidParam("类型不能为空");
        }
        List<String> signs=null;
        if(type.equals(userHadType)){
            if(openid==null || openid.equals("")){
                throw new InvalidParam("openid为空");
            }
            User user=userMapper.selectByOpenid(openid);
            if(user==null){
                throw new InvalidParam("openid无效");
            }
            signs=userSignMapper.selectNamesByOpenid(openid);
        }else if(type.equals(zcHadType)){
            if(zhongcaoid == null){
                throw new InvalidParam("种草id为空");
            }
            Zhongcao zc=zhongcaoMapper.selectByZhongcaoid(zhongcaoid);
            if(zc==null){
                throw new InvalidParam("种草id无效");
            }
            signs=userSignMapper.selectNamesByZhongcaoid(zhongcaoid);
        }
        return signs;
    }
}
